using System;
using UnityEngine;

public class UISplineSprite : UIWidget
{
	[HideInInspector]
	[SerializeField]
	private UIAtlas mAtlas;

	[HideInInspector]
	[SerializeField]
	private string mSpriteName;

	protected UISpriteData mSprite;

	private bool mSpriteSet;

	[NonSerialized]
	public BetterList<Vector3> points = new BetterList<Vector3>();

	protected Rect mInnerUV = default(Rect);

	protected Rect mOuterUV = default(Rect);

	public override Material material
	{
		get
		{
			return (!(mAtlas != null)) ? null : mAtlas.spriteMaterial;
		}
	}

	public override Vector4 drawingDimensions
	{
		get
		{
			Vector2 vector = base.pivotOffset;
			float num = (0f - vector.x) * (float)mWidth;
			float num2 = (0f - vector.y) * (float)mHeight;
			float num3 = num + (float)mWidth;
			float num4 = num2 + (float)mHeight;
			if (mSprite != null)
			{
				int paddingLeft = mSprite.paddingLeft;
				int paddingBottom = mSprite.paddingBottom;
				int num5 = mSprite.paddingRight;
				int num6 = mSprite.paddingTop;
				int num7 = mSprite.width + paddingLeft + num5;
				int num8 = mSprite.height + paddingBottom + num6;
				if (((uint)num7 & (true ? 1u : 0u)) != 0)
				{
					num5++;
				}
				if (((uint)num8 & (true ? 1u : 0u)) != 0)
				{
					num6++;
				}
				float num9 = 1f / (float)num7 * (float)mWidth;
				float num10 = 1f / (float)num8 * (float)mHeight;
				num += (float)paddingLeft * num9;
				num3 -= (float)num5 * num9;
				num2 += (float)paddingBottom * num10;
				num4 -= (float)num6 * num10;
			}
			Vector4 vector2 = border * atlas.pixelSize;
			float num11 = vector2.x + vector2.z;
			float num12 = vector2.y + vector2.w;
			float x = Mathf.Lerp(num, num3 - num11, mDrawRegion.x);
			float y = Mathf.Lerp(num2, num4 - num12, mDrawRegion.y);
			float z = Mathf.Lerp(num + num11, num3, mDrawRegion.z);
			float w = Mathf.Lerp(num2 + num12, num4, mDrawRegion.w);
			return new Vector4(x, y, z, w);
		}
	}

	public UIAtlas atlas
	{
		get
		{
			return mAtlas;
		}
		set
		{
			if (mAtlas != value)
			{
				RemoveFromPanel();
				mAtlas = value;
				mSpriteSet = false;
				mSprite = null;
				if (string.IsNullOrEmpty(mSpriteName) && mAtlas != null && mAtlas.spriteList.Count > 0)
				{
					SetAtlasSprite(mAtlas.spriteList[0]);
					mSpriteName = mSprite.name;
				}
				if (!string.IsNullOrEmpty(mSpriteName))
				{
					string text = mSpriteName;
					mSpriteName = string.Empty;
					spriteName = text;
					MarkAsChanged();
				}
			}
		}
	}

	public string spriteName
	{
		get
		{
			return mSpriteName;
		}
		set
		{
			if (string.IsNullOrEmpty(value))
			{
				if (!string.IsNullOrEmpty(mSpriteName))
				{
					mSpriteName = string.Empty;
					mSprite = null;
					mChanged = true;
					mSpriteSet = false;
				}
			}
			else if (mSpriteName != value)
			{
				mSpriteName = value;
				mSprite = null;
				mChanged = true;
				mSpriteSet = false;
			}
		}
	}

	public void ClearPoints()
	{
		points.Clear();
		MarkAsChanged();
	}

	public void AddPointWorld(Vector3 point)
	{
		Vector3 item = base.transform.parent.InverseTransformPoint(point);
		points.Add(item);
		MarkAsChanged();
	}

	public UISpriteData GetAtlasSprite()
	{
		if (!mSpriteSet)
		{
			mSprite = null;
		}
		if (mSprite == null && mAtlas != null)
		{
			if (!string.IsNullOrEmpty(mSpriteName))
			{
				UISpriteData sprite = mAtlas.GetSprite(mSpriteName);
				if (sprite == null)
				{
					return null;
				}
				SetAtlasSprite(sprite);
			}
			if (mSprite == null && mAtlas.spriteList.Count > 0)
			{
				UISpriteData uISpriteData = mAtlas.spriteList[0];
				if (uISpriteData == null)
				{
					return null;
				}
				SetAtlasSprite(uISpriteData);
				if (mSprite == null)
				{
					Debug.LogError(mAtlas.name + " seems to have a null sprite!");
					return null;
				}
				mSpriteName = mSprite.name;
			}
		}
		return mSprite;
	}

	protected override void OnInit()
	{
		base.OnInit();
	}

	protected override void OnUpdate()
	{
		base.OnUpdate();
		if (mChanged || !mSpriteSet)
		{
			mSpriteSet = true;
			mSprite = null;
			mChanged = true;
		}
	}

	protected void SetAtlasSprite(UISpriteData sp)
	{
		mChanged = true;
		mSpriteSet = true;
		if (sp != null)
		{
			mSprite = sp;
			mSpriteName = mSprite.name;
		}
		else
		{
			mSpriteName = ((mSprite == null) ? string.Empty : mSprite.name);
			mSprite = sp;
		}
	}

	public override void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
	{
		Texture texture = mainTexture;
		if (texture != null)
		{
			if (mSprite == null)
			{
				mSprite = atlas.GetSprite(spriteName);
			}
			if (mSprite == null)
			{
				return;
			}
			float num = 2f;
			float num2 = 2f;
			mOuterUV.Set((float)mSprite.x + num, (float)mSprite.y + num2, (float)mSprite.width - 2f * num, (float)mSprite.height - 2f * num2);
			mInnerUV.Set(mSprite.x + mSprite.borderLeft, mSprite.y + mSprite.borderTop, mSprite.width - mSprite.borderLeft - mSprite.borderRight, mSprite.height - mSprite.borderBottom - mSprite.borderTop);
			mOuterUV = NGUIMath.ConvertToTexCoords(mOuterUV, texture.width, texture.height);
			mInnerUV = NGUIMath.ConvertToTexCoords(mInnerUV, texture.width, texture.height);
		}
		SimpleFill(verts, uvs, cols);
	}

	protected void SimpleFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
	{
		if (points == null || points.Count == 0)
		{
			return;
		}
		Vector2 item = new Vector2(mOuterUV.xMin, mOuterUV.yMin);
		Vector2 item2 = new Vector2(mOuterUV.xMax, mOuterUV.yMax);
		Vector3 vector = Vector3.zero;
		bool flag = false;
		for (int i = 0; i < points.Count - 1; i++)
		{
			Vector3 p = ((i != 0) ? points[i - 1] : points[i]);
			Vector3 p2 = points[i];
			Vector3 vector2 = points[i + 1];
			Vector3 p3 = ((i + 2 >= points.Count) ? vector2 : points[i + 2]);
			int num = 4;
			for (int j = 0; j < num; j++)
			{
				Vector3 vector3 = Vector3Ex.CatmullRomLerp(p, p2, vector2, p3, (float)j / (float)num);
				vector3.z = 0f;
				Vector3 vector4 = Vector3Ex.CatmullRomLerp(p, p2, vector2, p3, (float)(j + 1) / (float)num);
				vector4.z = 0f;
				Vector3 normalized = (vector4 - vector3).normalized;
				Vector3 vector5 = new Vector3(normalized.y, 0f - normalized.x) * mHeight * 0.5f;
				if (!flag)
				{
					vector = vector5;
				}
				verts.Add(vector3 - vector);
				verts.Add(vector3 + vector);
				verts.Add(vector4 + vector5);
				verts.Add(vector4 - vector5);
				uvs.Add(item);
				uvs.Add(new Vector2(item.x, item2.y));
				uvs.Add(item2);
				uvs.Add(new Vector2(item2.x, item.y));
				Color color = base.color;
				color.a = finalAlpha;
				Color32 item3 = ((!atlas.premultipliedAlpha) ? color : NGUITools.ApplyPMA(color));
				cols.Add(item3);
				cols.Add(item3);
				cols.Add(item3);
				cols.Add(item3);
				vector = vector5;
				flag = true;
			}
		}
	}
}
